package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.rank.cross.CrossActivityTeamRank;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityTeamRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityTeamRankModelGroup {
    private int activityId;
    private int groupId;
    //key conditionType
    private Map<Integer, CrossActivityTeamRankListModel> crossTeamRankModelMap = new ConcurrentHashMap<>();

    public ActivityTeamRankModelGroup(int activityId, int groupId) {
        this.activityId = activityId;
        this.groupId = groupId;
    }

    public int getActivityId() {
        return activityId;
    }

    public int getGroupId() {
        return groupId;
    }

    public Map<Integer, CrossActivityTeamRankListModel> getCrossTeamRankModelMap() {
        return crossTeamRankModelMap;
    }

    public void setCrossTeamRankModelMap(Map<Integer, CrossActivityTeamRankListModel> crossTeamRankModelMap) {
        this.crossTeamRankModelMap = crossTeamRankModelMap;
    }

    public void initRankList(List<CrossActivityTeamRank> tempRankList) {
        Map<Integer, CrossActivityTeamRankListModel> tempModelMap = new ConcurrentHashMap<>();
        Map<Integer, LinkedList<CrossActivityTeamRank>> tempRankListMap = new ConcurrentHashMap<>();
        for (CrossActivityTeamRank teamRank : tempRankList) {
            int conditionType = teamRank.getBigRankType();
            LinkedList<CrossActivityTeamRank> list = tempRankListMap.get(conditionType);
            if (list == null) {
                list = new LinkedList<>();
                tempRankListMap.put(conditionType, list);
            }
            list.add(teamRank);
        }

        for (Map.Entry<Integer, LinkedList<CrossActivityTeamRank>> entry : tempRankListMap.entrySet()) {
            int conditionType = entry.getKey();
            LinkedList<CrossActivityTeamRank> list = entry.getValue();
            CrossActivityTeamRankListModel model = new CrossActivityTeamRankListModel(activityId, groupId, conditionType, true);
            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityId));
            if (maxRank <= 0) {
                maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
            }
            model.setRankNum(maxRank);
            model.setRankList(entry.getValue());
            tempModelMap.put(conditionType, model);
        }
        crossTeamRankModelMap = tempModelMap;
    }

    public CrossActivityTeamRankListModel getRankModelOrNull(int conditionType) {
        return crossTeamRankModelMap.get(conditionType);
    }

    public CrossActivityTeamRankListModel getRankModelNewIfNull(int conditionType) {
        CrossActivityTeamRankListModel listModel = crossTeamRankModelMap.get(conditionType);
        if (listModel == null) {
            synchronized (crossTeamRankModelMap) {
                listModel = crossTeamRankModelMap.get(conditionType);
                if (listModel == null) {
                    listModel = new CrossActivityTeamRankListModel(activityId, groupId, conditionType, true);
                    int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityId));
                    if(maxRank <= 0){
                        maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                    }
                    listModel.setRankNum(maxRank);
                    crossTeamRankModelMap.put(conditionType, listModel);
                }
            }
        }
        return listModel;
    }

    public void save() {
        for (CrossActivityTeamRankListModel listModel : crossTeamRankModelMap.values()) {
            listModel.save();
        }
    }
}
